package com.google.code.etrclient.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import com.byarger.exchangeit.EasySSLSocketFactory;
import com.google.code.etrclient.pojo.Course;

public class ServiceCalls {
  /**
   * Logs in to the web service and returns the first page as a string
   * 
   * @param username The username to log in with
   * @param password The password of the user
   * @param language The language of the web interface
   * @return Cleaned and truncated HTML of the resulting page (hopefully the first page)
   */
  public static String loginAndGetFirstPage(String username, String password, String language) {  
    String res = "";
    
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
    HttpParams params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
    
    //HttpPost httppost = new HttpPost(BASE_URL + LOGIN_PAGE);
    HttpPost httppost = new HttpPost("http://10.15.1.5/proba/etr.html");
    String httpRes = "";
        
    try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      nameValuePairs.add(new BasicNameValuePair("uname", username));
      nameValuePairs.add(new BasicNameValuePair("pwd", password));
      nameValuePairs.add(new BasicNameValuePair("nyelv", language));
      nameValuePairs.add(new BasicNameValuePair("_tokentoken", ""));
      nameValuePairs.add(new BasicNameValuePair("submit.x", "0"));
      nameValuePairs.add(new BasicNameValuePair("render", "false"));
      nameValuePairs.add(new BasicNameValuePair("office", "OLD"));
      //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
      HttpResponse response = httpClient.execute(httppost);
      String responseStr = Helper.decodeResponse(response);
      //Log.d("RESPONSE", responseStr);
      //tvMain.setText(responseStr);
      httpRes = responseStr;
      
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    ArrayList<Course> courses = Helper.parseCoursesXMLtoPOJO(Helper.cleanAndTruncCoursesHtml(httpRes));
    for (Course c : courses) {
      res += c.toString();
      res += "\n";
    }
        
    return res;
    
  }
}
